package com.woniu.zsd.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.UserDto;
import com.woniu.form.AddAddressForm;
import com.woniu.form.UpdateAddressForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.commons.exception.ZsdException;
import com.woniu.zsd.user.entity.User;
import com.woniu.zsd.user.entity.UserAddress;
import com.woniu.zsd.user.entity.UserInfo;
import com.woniu.zsd.user.mapper.UserAddressMapper;
import com.woniu.zsd.user.service.IUserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    @Override
    public UserAddress addUserAddress(AddAddressForm aaf, String id) {
        UserAddress userAddress = new UserAddress();
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        userAddress.setAddress(aaf.getAddress());
        userAddress.setLongitude(aaf.getLongitude());
        userAddress.setLatitude(aaf.getLatitude());
        if(!queryWrapper.isEmptyOfEntity()){
            userAddress.setDefaultMark("1");
        }else{
            userAddress.setDefaultMark("0");
        }
        userAddress.setUserId(Integer.parseInt(id));
        baseMapper.insert(userAddress);
        return userAddress;
    }

    @Override
    public UserAddress updateUserAddress(UpdateAddressForm uaf, String id) {
        UserAddress userAddress = new UserAddress();
        UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", id).eq("id", uaf.getId());

        // 如果传入了地址，则更新地址
        if (uaf.getAddress() == null|| uaf.getAddress().isEmpty()) {
            QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", id).eq("id", uaf.getId());
            userAddress.setAddress(baseMapper.selectOne(queryWrapper).getAddress());
        }else{
            updateWrapper.set("address", uaf.getAddress());
            userAddress.setAddress(uaf.getAddress());
        }

        // 处理默认地址逻辑
        if (uaf.getDefaultMark() != null) {
            if (uaf.getDefaultMark().equals("1")) {
                // 先将该用户的所有地址的 default_mark 设置为 0
                UpdateWrapper<UserAddress> clearDefaultWrapper = new UpdateWrapper<>();
                clearDefaultWrapper.eq("user_id", id).set("default_mark", "0");
                baseMapper.update(null, clearDefaultWrapper);

                // 再将当前地址的 default_mark 设置为 1
                updateWrapper.set("default_mark", "1");
            } else if (uaf.getDefaultMark().equals("0")) {
                updateWrapper.set("default_mark", "0");
            }
        }
        // 执行更新操作
        baseMapper.update(userAddress, updateWrapper);
        return userAddress;
    }

    @Override
    public List<UserAddress> selAll(String id) {
        // 创建查询条件
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id); // 根据用户 ID 查询
        // 执行查询
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public UserAddress getUserAddress(String id) {
        UserAddress userAddress = baseMapper.selectById(id);
        if (userAddress == null) {
            throw new ZsdException("地址不存在", 500);
        }
        return userAddress;
    }
}
